package main

import (
	"github.com/labstack/echo/v4"
	"html/template"
	"io"
	"net/http"
	"time"
)

func main() {
	//加载模板
	t := &Template{
		templates: template.Must(template.ParseGlob("templates/views/*.html")),
	}
	e := echo.New()
	e.Renderer = t

	e.GET("/hello", Hello)
	e.GET("/car", Car)
	e.Start(":80")

}

func Hello(c echo.Context) error {
	//对应html中的模板名字
	return c.Render(http.StatusOK, "hello.html", map[string]interface{}{
		"title": "Hello, World",
	})
}
func Car(c echo.Context) error {
	//对应html中的模板名字
	return c.Render(http.StatusOK, "car.html", map[string]interface{}{
		"title": "CAR",
		"time":  time.Now().Format("2006-01-02 15:04:05"),
	})
}

// Template
// @Description: 实现 echo.Renderer 接口
type Template struct {
	templates *template.Template
}

func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
	return t.templates.ExecuteTemplate(w, name, data)
}
